<?php

// namespace Home\Action;

// use Think\Action;

require 'Class/Aliyun/aliyun.php';

use \Aliyun\OSS\OSSClient;

class AliyunAction extends Action{
// oss-internal.aliyuncs.com 
// oss-cn-qingdao-internal.aliyuncs.com
	public $client;
	public $bucket;
	public $url;
	public $w_hz_host = '.oss-cn-hangzhou.aliyuncs.com/';
	public $host = '.oss-internal.aliyuncs.com/';
	public $qd_host = '.oss-cn-qingdao-internal.aliyuncs.com/';
	public function __construct(){

		parent::__construct();

		$this->client = OSSClient::factory(array(
		    'AccessKeyId' => '',
		    'AccessKeySecret' => '',
		));
		// echo '<pre>';

		$this->bucket = 'missql-web';
		$this->url = 'http://'.$this->bucket.$this->w_hz_host;
	}

	public function index(){
	
		// $this->display();
		// $url = $this->uploadPut( '/hengjia/test' , '11' , strlen('11') );
		// echo $url;
	}

	public function create(){
		// var_dump($this->client);
		// $res = $this->client->createBucket(array(
  //   		'Bucket' => 'missqltest',
		// ));
		// var_dump($res);
	}

	/**
	 * ueditor上传文件
	 * 存储到数据库
	 * @return [type] [description]
	 */
	public function ueditorUp(){
	    //获取存储目录
	    if ( isset( $_GET[ 'fetch' ] ) ) {
	        header( 'Content-Type: text/javascript' );
	        echo 'updateSavePath('. json_encode('/') .');';
	        return;
	    }

    	$file 		= $_FILES['upfile'];
    	$file_name 	= $file['name'];
    	$tmp 		= $file['tmp_name'];
    	$size 		= $file['size'];
    	// 以网站名称存储


    	$save_path = '/hengjia/img/'.date("Ymd").'/'
    		.time().
    		$this->getFileExt($file_name);

    	// echo $save_path;exit;
    	// 
		$content = '';
		$length  = 0;

		$fp = fopen( $tmp ,'r');
        if($fp){
            $f = fstat($fp);
            $length = $f['size'];
            while(!feof($fp)){
                $content .= fgets($fp,8192);
            }
        }
        fclose($fp);

        $url = $this->uploadPut( $save_path , $content , $length );

        echo $url;

    	exit;
    	if(! empty($res) ){
    		$json = array(
    			'url' => $this->url.$save_path,
    			'title' => $file_name,
    			'original' => $file_name,
    			'state' => 'SUCCESS',
    		);

    		$data = array(
    			'user_id' => 0,
    			'term_id' => 0,
    			'object_id' => 0,
    			'cloud_type' => 'upyun',
    			'key' => $save_path,
    			'file_name' => $file_name,
    			'file_type' => $this->getFileExt($file_name),
    			'url' => $this->url.$save_path,
    			'size' => $size,
    			'create_time' => time()
    		);

    		$fid = M('cloudfile')->add($data);

    		if($fid){
    			echo json_encode($json);
    		}else{
    			echo json_encode(array('state'=>'写入数据库错误'));
    		}
    		
    	}else{
    		echo json_encode(array('state'=>'上传错误'));
    	}

	}


	public function upload(){
// Array ( [file] => Array ( [name] => iphone.png [type] => image/png [tmp_name] => /tmp/phpbonBXQ [error] => 0 [size] => 45671 ) )

		$file_name = $_FILES['file']['name'];
		$file_type = $_FILES['file']['type'];
		$tmp_name  = $_FILES['file']['tmp_name'];
		$size 	   = $_FILES['file']['size'];
		$allow_type = array(
			'image/jpg',
			'image/jpeg',
			'image/png',
			'image/gif',
			'audio/ogg',
			'audio/mp3',
			'video/mp4',
			'text/plain',
		);
		// echo $file_type;
		if (!in_array($file_type, $allow_type)){
			exit('error type');
		}
		if ( in_array( $file_type , array('image/jpg','image/jpeg','image/png','image/gif') )){
			$save_path = 'images';
		}elseif ( in_array( $file_type , array('audio/ogg','audio/mp3') )){
			$save_path = 'audio';
		}elseif ( in_array( $file_type , array('video/mp4') )){
			$save_path = 'video';
		}else{
			$save_path = 'other';
		}

		$content = '';
		$length  = 0;

		$fp = fopen( $tmp_name ,'r');
        if($fp){
            $f = fstat($fp);
            $length = $f['size'];
            while(!feof($fp)){
                $content .= fgets($fp,8192);
            }
        }
        fclose($fp);

        $url = $this->uploadPut( $save_path.'/'.$file_name , $content , $length );

        echo $url;

        // 使用分块上传可能会快点 读一点上传一点
	}

	public function uploadPut( $key , $content , $length ){
		$res = $this->client->putObject(array(
		    'Bucket' => $this->bucket,
		    'Key' => $key,
		    'Content' => $content,
		    'ContentLength' => $length,
		));
		$file_url = $this->url.$key;
		return $file_url;
	}

	

	public function put(){
		$path = 'Public/audio/';
		$file = 'ED6563.ogg';
		$file_path = $path.$file;
		$fp = fopen( $file_path , 'r');
		$size = filesize( $file_path );
		$key = $file;
		$res = $this->client->putObject(array(
		    'Bucket' => $this->bucket,
		    'Key' => $key,
		    'Content' => $fp,
		    'ContentLength' => $size,
		));
		$file_url = $this->url.$key;
		echo "<audio src='$file_url'></audio>";
		// echo '<a href="'.$file_url.'">go</a>';
		// var_dump($res);
	}

	public function multiput(){
		// 需要支持断点上传。
		// 上传超过100MB大小的文件。
		// 网络条件较差，和OSS的服务器之间的链接经常断开。
		// 需要流式地上传文件。
		// 上传文件之前，无法确定上传文件的大小。

	}

    /**
     * 获取文件扩展名
     * @return string
     */
    private function getFileExt($name)
    {
        return strtolower( strrchr( $name , '.' ) );
    }

}// -END

